﻿
Imports System
Imports System.Data
Imports System.String
Imports System.Text
Imports System.Collections

Public Class Frm_OFSCheckOrde

    Implements IDServer.IDServer.IDServer

    Private MasterDS As DataSet
    Private DetailDS As DataSet
    Private WithEvents MainForm As Frm_MainForm


#Region "Property"

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#End Region



    Public FindBool As Boolean = True

#Region "interface"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

    Public Fields As String

#End Region


#Region "function or sub"

    Private Sub BindMasterGrid()

        Dim mTable As New DataTable
        Try
            With mTable
                .Columns.Add("ShopNo")
                .Columns.Add("fSaleOrdeNo")
                .Columns.Add("fWriteNo")
                .Columns.Add("fDeliverMode")
                .Columns.Add("fDeliverDate")
                .Columns.Add("fStoreCode")
                .Columns.Add("fGoodsType")
                .Columns.Add("fDeliverId")
                .Columns.Add("Num")
                .Columns.Add("fCustomerType")
                .Columns.Add("fCustomerName")
                .Columns.Add("fLinkMan")
                .Columns.Add("fLinkPhone")
                .Columns.Add("fDeliverAddress")
                .Columns.Add("fEmployeeID")
                .Columns.Add("fSaleDate")
                .Columns.Add("fOperaterMan")
                .Columns.Add("fOperateDate")
                .Columns.Add("fCheckMan")
                .Columns.Add("fCheckDate")
                .Columns.Add("fOrdeStatus")
                .Columns.Add("fRemark")

            End With
            MasterDS = New DataSet
            MasterDS.Tables.Add(mTable)
            Me.DGV_Master.DataSource = MasterDS.Tables(0)

            With Me.DGV_Master
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHashTable(0).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With
        Catch ex As Exception
            MsgBox("BindMasterGrid", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable = Nothing
        End Try

    End Sub

    Private Sub BindDetailGrid()

        Dim mTable As New DataTable
        Try
            With mTable
                .Columns.Add("ItemNo")
                .Columns.Add("fGoodsBarcode")
                .Columns.Add("Code")
                .Columns.Add("SupperItem")
                .Columns.Add("IsPur")
                .Columns.Add("IsDeliver")
                .Columns.Add("Cnname")
                .Columns.Add("Color")
                .Columns.Add("Spec")
                .Columns.Add("fSumCount")
                .Columns.Add("Qty")
                .Columns.Add("fTotal")
            End With
            DetailDS = New DataSet
            DetailDS.Tables.Add(mTable)
            Me.DGV_Detail.DataSource = DetailDS.Tables(0)

            With Me.DGV_Detail
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHashTable(1).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With

        Catch ex As Exception
            MsgBox("BindDetailGrid", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable = Nothing
        End Try

    End Sub

    Private Function BindHashTable(Optional ByVal Index As Integer = 0) As Hashtable

        Dim mHash As New Hashtable
        Try

            With mHash
                Select Case Index
                    Case 0
                        .Add("ShopNo", "門市")
                        .Add("fSaleOrdeNo", "銷售單號")
                        .Add("fWriteNo", "手寫單號")
                        .Add("fDeliverMode", "送貨模式")
                        .Add("fDeliverDate", "送貨日期")
                        .Add("fStoreCode", "到聯門市")
                        .Add("fGoodsType", "單類")
                        .Add("Num", "采購單號")
                        .Add("fDeliverId", "送貨單號")
                        .Add("fCustomerType", "客戶類型")
                        .Add("fCustomerName", "客戶名稱")
                        .Add("fLinkMan", "聯系人")
                        .Add("fLinkPhone", "聯系電話")
                        .Add("fDeliverAddress", "送貨地址")
                        .Add("fEmployeeID", "銷售人員")
                        .Add("fSaleDate", "銷售日期")
                        .Add("fOperaterMan", "操作人員")
                        .Add("fOperateDate", "操作日期")
                        .Add("fCheckMan", "審核人員")
                        .Add("fCheckDate", "審核日期")
                        .Add("fOrdeStatus", "單據狀態")
                        .Add("fRemark", "備注")

                    Case 1
                        .Add("ItemNo", "編號")
                        .Add("fGoodsBarcode", "貨品條碼")
                        .Add("Code", "編碼")
                        .Add("SupperItem", "供應商編號")
                        .Add("IsPur", "是否已下采購單")
                        .Add("IsDeliver", "是否已安排送貨")
                        .Add("Cnname", "産品描述")
                        .Add("Color", "顔色")
                        .Add("Spec", "規格")
                        .Add("fSumCount", "銷售數量")
                        .Add("Qty", "采購數量")
                        .Add("fTotal", "送貨數量")
                End Select
            End With
            Return mHash
        Catch ex As Exception
            Return Nothing
            MsgBox("BindHashTable" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mHash = Nothing
        End Try

    End Function




#End Region



    Private Sub Frm_OFSCheckOrde_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            BindMasterGrid()
            BindDetailGrid()
            Me.DateTimePicker1.Value = Date.Now
            Me.DateTimePicker2.Value = Date.Now
        Catch ex As Exception
            MsgBox(My.resources.LoadFormError, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            'Me.Cursor = Cursors.WaitCursor
            Dim sql As String = ""
            Dim WhereStr As String = ""

            If Me.TextBox1.Text.Length > 0 Then
                WhereStr += " and a.fSaleOrdeNo='" & Me.TextBox1.Text.Trim & "'"
            End If

            If Me.TextBox2.Text.Length > 0 Then
                WhereStr += " and a.fWriteNo='" & Me.TextBox2.Text.Trim & "'"
            End If

            If Me.DateTimePicker1.Value.ToString.Length > 0 And Me.DateTimePicker2.Value.ToString.Length <= 0 Then

            ElseIf Me.DateTimePicker1.Value.ToString.Length <= 0 And Me.DateTimePicker2.Value.ToString.Length >= 0 Then

            ElseIf Me.DateTimePicker1.Value.ToString.Length >= 0 And Me.DateTimePicker2.Value.ToString.Length >= 0 Then
                WhereStr += " and a.fSaleDate between '" & Format(Me.DateTimePicker2.Value.ToString("yyyy-MM-dd"), "yyyy-MM-dd") & "' and '" & Format(Me.DateTimePicker1.Value.ToString("yyyy-MM-dd"), "yyyy-MM-dd") & "'"
            End If


            If Me.TBDeliverNO.Text <> "" Then
                WhereStr += " and g.fDeliverId = '" & Me.TBDeliverNO.Text.Trim & "' "
            End If

            If Me.TBPONum.Text <> "" Then
                WhereStr += "and h.Num = '" & Me.TBPONum.Text.Trim & "' "
            End If

            If Me.ComboBox1.Text = "未下采購單" Then
                WhereStr += "and isnull (h.Num,'')=''"
            Else
                If Me.ComboBox1.Text = "已下采購單" Then
                    WhereStr += "and isnull (h.Num,'')<>''"
                End If
            End If

            If Me.ComboBox2.Text = "未安排送貨" Then
                WhereStr += "and isnull(g.fDeliverId,'')=''"
            Else
                If Me.ComboBox2.Text = "已安排送貨" Then
                    WhereStr += "and isnull(g.fDeliverId,'')<>''"
                End If
            End If

            sql = "select distinct c.fShortName as ShopNo ,a.FSaleOrdeNO ,fWriteNO, d.listA as  fDeliverMode, fDeliverDate ,  " & vbCrLf
            sql &= "e.fCName as  fGoodsType ,fCustomerName,fLinkMan,fLinkPhone,fDeliverAddress,a.fEmployeeID,fSaleDate,fOPeraterMan," & vbCrLf
            sql &= "a.fOperateDate,fCheckMan,fCheckDate,f.ListA as fOrdeStatus, a.fRemark,isnull(g.fDeliverId,'') as fDeliverId ,isnull (h.Num,'')as Num " & vbCrLf
            sql &= "from Sal_SaleOrdeA a  left join Sal_SaleOrdeB b on a.fSaleOrdeNo = b.FSaleOrdeNo " & vbCrLf
            sql &= "left join BSC_StoreHouse c on a.ShopNo = c.fCode  left join BSC_OtherDefineB d on a.fDeliverMode = d.ListCode "
            sql &= "left join BSC_GoodsType e on a.FGoodsType= e.fCode   left join BSC_OtherDefineB f on a.fOrdeStatus  = f.ListCode "
            sql &= "and f.ItemNo ='16'  left join DeliverGoodsDetail g on a.fsaleordeNo = g.fsaleordeno  "
            sql &= "left join Pur_POB h on b.fGoodsBarCode = h.BarCode and isnull(h.SaleOrdeNo,'')<>'' "
            sql &= " where  1=1 " & WhereStr
            MasterDS = ReturnDataset(sql)
            Me.DGV_Master.DataSource = MasterDS.Tables(0)
        Catch ex As Exception
            MsgBox("Button1_Click:" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    Private Sub DGV_Master_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Master.CellClick

    End Sub

    Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown

    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress


    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged

    End Sub

    Private Sub DGV_Master_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGV_Master.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DataGridViewstorage_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DGV_Detail_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGV_Detail.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DataGridViewstorage_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DGV_Master_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DGV_Master.Paint
        FormatDgrd(DGV_Master, "fOrdeStatus")
    End Sub

    Private Sub DGV_Master_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGV_Master.SelectionChanged
        Dim sql As String = ""
        Dim fSaleOrdeNo As String = ""
        Try
            fSaleOrdeNo = Me.DGV_Master.CurrentRow.Cells("fSaleOrdeNo").Value
            sql = "select b.ItemNo,replace(b.fGoodsBarcode,'*','') as fGoodsBarCode,e.Code,e.SupperItem,e.Cnname," & vbCrLf
            sql += " IsPur=case when isnull(c.barcode,'')='' then '未下采購' when isnull(c.barcode,'')<>'' then '已下采購' end,isnull(c.Qty,0.00) as Qty," & vbCrLf
            sql += " isDeliver=case when isnull(d.fGoodsBarCode,'')='' then '未送貨' when isnull(d.fGoodsBarCode,'')<>'' then '已送貨' end,isnull(d.fTotal,0.00)as fTotal ,g.ColorSName as Color,h.SizeSName as Spec,b.fSumCount " & vbCrLf
            sql += " from dbo.Sal_SaleOrdeA a inner join Sal_SaleOrdeB b on a.fSaleOrdeNo=b.fSaleOrdeNo left join " & vbCrLf
            sql += " (select SaleOrdeNo,Barcode,sum(Qty) as Qty from dbo.Pur_POB where isnull(SaleOrdeNo,'')<>'' group by SaleOrdeNo,Barcode) " & vbCrLf
            sql += " c on  b.fGoodsBarCode=c.Barcode left join " & vbCrLf
            sql += " (select ItemNo,fSaleOrdeNo,fGoodsBarcode,sum(fTotal) as fTotal from DeliverGoodsInfo where isnull(fSaleOrdeNo,'')<>'' group by ItemNo,fSaleOrdeNo,fGoodsBarcode)" & vbCrLf
            sql += " d on a.fSaleOrdeNo=d.fSaleOrdeNo and b.fGoodsBarCode=d.fGoodsBarcode and b.ItemNo = D.ItemNo left join dbo.BSC_Goods e on b.fGoodsBarcode=e.BarCode" & vbCrLf
            sql += " left join dbo.BSC_ColorDefine g on e.Color=g.ColorCode left join dbo.BSC_SizeDefine h on e.Spec=h.SizeCode where a.fSaleOrdeNo='" & fSaleOrdeNo & "'" & vbCrLf
            sql += " Order By b.ItemNo"
            DetailDS = ReturnDataset(sql)

            Me.DGV_Detail.DataSource = DetailDS.Tables(0)
        Catch ex As Exception
            MsgBox("DGV_Master_CellContentClick:" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
End Class
